//-----------------------------------------------------------------------
// <copyright file="SanPhamDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class SanPhamDAOBase
    {
        #region Common methods
        public virtual SanPham CreateSanPhamFromReader(IDataReader reader)
        {
            SanPham item = new SanPham();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaSanPham"))) item.MaSanPham = (string)reader["MaSanPham"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenSanPham"))) item.TenSanPham = (string)reader["TenSanPham"];
                if (!reader.IsDBNull(reader.GetOrdinal("SoDiem"))) item.SoDiem = (int)reader["SoDiem"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) item.NoiDung = (string)reader["NoiDung"];
                if (!reader.IsDBNull(reader.GetOrdinal("ChiTiet"))) item.ChiTiet = (string)reader["ChiTiet"];
                if (!reader.IsDBNull(reader.GetOrdinal("DanhMuc"))) item.DanhMuc = (int)reader["DanhMuc"];
                if (!reader.IsDBNull(reader.GetOrdinal("AnhDaiDien"))) item.AnhDaiDien = (string)reader["AnhDaiDien"];
                if (!reader.IsDBNull(reader.GetOrdinal("AnhLon"))) item.AnhLon = (string)reader["AnhLon"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateSanPhamFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateSanPham methods
            
        public virtual void CreateSanPham(SanPham sanPham)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSanPhamInsert");
                
				database.AddInParameter(dbCommand, "@MaSanPham", DbType.String, sanPham.MaSanPham);
				database.AddInParameter(dbCommand, "@TenSanPham", DbType.String, sanPham.TenSanPham);
				database.AddInParameter(dbCommand, "@SoDiem", DbType.Int32, sanPham.SoDiem);
				database.AddInParameter(dbCommand, "@NoiDung", DbType.String, sanPham.NoiDung);
				database.AddInParameter(dbCommand, "@ChiTiet", DbType.String, sanPham.ChiTiet);
				database.AddInParameter(dbCommand, "@DanhMuc", DbType.Int32, sanPham.DanhMuc);
				database.AddInParameter(dbCommand, "@AnhDaiDien", DbType.String, sanPham.AnhDaiDien);
				database.AddInParameter(dbCommand, "@AnhLon", DbType.String, sanPham.AnhLon);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateSanPhamException, ex);
            }
        }

        #endregion
		
        #region UpdateSanPham methods
        
        public virtual void UpdateSanPham(SanPham sanPham)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSanPhamUpdate");            
                
                database.AddInParameter(dbCommand, "@MaSanPham", DbType.String, sanPham.MaSanPham);
                database.AddInParameter(dbCommand, "@TenSanPham", DbType.String, sanPham.TenSanPham);
                database.AddInParameter(dbCommand, "@SoDiem", DbType.Int32, sanPham.SoDiem);
                database.AddInParameter(dbCommand, "@NoiDung", DbType.String, sanPham.NoiDung);
                database.AddInParameter(dbCommand, "@ChiTiet", DbType.String, sanPham.ChiTiet);
                database.AddInParameter(dbCommand, "@DanhMuc", DbType.Int32, sanPham.DanhMuc);
                database.AddInParameter(dbCommand, "@AnhDaiDien", DbType.String, sanPham.AnhDaiDien);
                database.AddInParameter(dbCommand, "@AnhLon", DbType.String, sanPham.AnhLon);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateSanPhamException, ex);
            }
        }
        
        #endregion
		
        #region DeleteSanPham methods
        public virtual void DeleteSanPham(string maSanPham)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSanPhamDelete");
                
                database.AddInParameter(dbCommand, "@MaSanPham", DbType.String, maSanPham);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteSanPhamException, ex);
            }
        }

        #endregion
		
        #region GetSanPham methods
        
        public virtual SanPham GetSanPham(string maSanPham)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSanPhamGet");
                
                database.AddInParameter(dbCommand, "@MaSanPham", DbType.String, maSanPham);
                
                SanPham sanPham =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        sanPham = CreateSanPhamFromReader(reader);
                        reader.Close();
                    }
                }
                return sanPham;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetSanPhamException, ex);
            }
        }
        
        #endregion
		
        #region GetSanPhamList methods
        public virtual SanPhamCollection GetSanPhamList(SanPhamColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSanPhamGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                SanPhamCollection sanPhamCollection = new SanPhamCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        SanPham sanPham = CreateSanPhamFromReader(reader);
                        sanPhamCollection.Add(sanPham);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return sanPhamCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetSanPhamListException, ex);
            }
        }
        
        public virtual SanPhamCollection GetSanPhamList(SanPhamColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetSanPhamList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}